package org.omnifaces.test;

import static java.lang.System.out;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class Utils {
	
	public static final long ONE_SECOND = 1000;
	public static final long TWO_SECONDS = 2000;
	
	private Utils() {
	}
	
	/**
	 * Parse the default format as returned by {@link Date#toString()}, e.g. Sat Jun 29 15:28:40 CEST 2013
	 * 
	 * @param source
	 * @return
	 */
	public static Date getDate(String source) {
		try {
			return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(source);
		} catch (ParseException e) {
			throw new IllegalStateException(e);
		}
	}
	
	public static void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
		}
	}
	
	public static void print(String text) {
		out.println(new Date().toString() + " - " + Thread.currentThread().getId() + ":" + text);
	}
	
	public static void printVar(String name, Object value) {
		print(name + " = " + value);
	}
	
	public static <T> T instance(Class<T> clazz) {
		try {
			return clazz.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			throw new IllegalStateException(e);
		}
	}

}
